package com.chaos.eurekaproducer.controller;

import com.chaos.eurekaproducer.domain.User;
import com.chaos.eurekaproducer.listener.MyEvent;
import com.chaos.eurekaproducer.service.IUserService;
import com.chaos.starter.ChaosEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

/**
 * @author liaopeng
 * @title: UserController
 * @projectName eureka-producer
 * @description: TODO
 * @date 2021/5/14下午6:04
 */
@RestController
@RequestMapping("/user")
class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private ChaosEntity chaosEntity;
    /**
     * 测试事件监听
     */
    @RequestMapping("/eventPublish")
    public void eventPublish(){
        User user = new User();
        user.setName("chaos");
        user.setAge(1);
        MyEvent myEvent = new MyEvent(this, user);
        System.out.println("发布事件");
        applicationContext.publishEvent(myEvent);
    }

    /**
     * 测试多线程中事务一致性问题
     * TODO
     */
    @RequestMapping("/getUser")
    public ChaosEntity getUser(){
        return chaosEntity;
    }


    /**
     * 测试多线程中事务一致性问题
     * TODO
     */
    @RequestMapping("/insert")
    public void insertUsers(){
        User user = new User();
        user.setName("夏夜");
        user.setAge(18);
        System.out.println(userService.getClass());
        userService.insert(user);
        System.out.println("夏夜");
    }

    public static void main(String[] args) {
        try {
//            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
//            LocalDate date = LocalDate.parse(String.valueOf(202002240000L), dtf);
            LocalDate date = LocalDate.of(2021, 10, 13);
//            LocalDate now = LocalDate.now();
            LocalDate now = LocalDate.of(2022, 2, 15);
            long day = now.toEpochDay() - date.toEpochDay();
            System.out.println(day);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
